//
//  SHPictureView.swift
//  WeiBo001
//
//  Created by ZhiQiang Li on 16/7/31.
//  Copyright © 2016年 ZhiQiang Li. All rights reserved.
//

import UIKit
//可重用标示符
private let SHPictureViewCellId = "SHPictureViewCellId"
// cell间距
let cellMargin: CGFloat = 5
// cell宽高
let cellWH: CGFloat = 80

class SHPictureView: UICollectionView {
    //创建一个模型
    var pictureInfo:[CZPictureInfo]?{
        didSet{
            // countLabel赋值
            countLabel.text = "\(pictureInfo?.count ?? 0)"
            // 获得当前配图的size
            let size = getPictureViewSize(pictureInfo?.count ?? 0)
            // 更新当前配图的约束
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
            // 刷新UI
            reloadData()
        }

        }
    // 计算配图的size
    func getPictureViewSize(count: Int) -> CGSize{
        
        // 计算出行数和列数
        let col = count == 4 ? 2 : count >= 3 ? 3 : count
        let row = count == 4 ? 2 : (count - 1)/3 + 1
        // 计算size 中的宽和高
        let w = CGFloat(col) * cellWH + CGFloat(col - 1)*cellMargin
        let h = CGFloat(row) * cellWH + CGFloat(row - 1)*cellMargin
        
        
        return CGSize(width: w, height: h)
    }
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置视图
    private func setupUI(){
        backgroundColor = RandomColor()
        
        // 获取layout
        let layout = self.collectionViewLayout as!UICollectionViewFlowLayout
        // 设置itemSize
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        // 设置水平和垂直间距
        layout.minimumInteritemSpacing = cellMargin
        layout.minimumLineSpacing = cellMargin
        // 注册cell
        registerClass(SHPictureViewCell.self, forCellWithReuseIdentifier: SHPictureViewCellId )
        // 设置数据源代理
        dataSource = self
        // 添加控件
        addSubview(countLabel)
        
        // 添加约束
        countLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
    }
    
    
    // -  懒加载
   
    private lazy var countLabel: UILabel = UILabel(textColor: UIColor.redColor(), textFont: 35)
    }
extension SHPictureView: UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureInfo?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(SHPictureViewCellId, forIndexPath: indexPath) as! SHPictureViewCell
        // cell赋值
        cell.pic_url = pictureInfo![indexPath.item]
        return cell
    }
}

// 自定义cell
class SHPictureViewCell: UICollectionViewCell {
    // 定义一个模型 供外界赋值
    var pic_url: CZPictureInfo?{
        didSet{
            // 设置image
            bgImageView.sh_setImage(pic_url?.thumbnail_pic, placeholderImgName: "weibo_placeholder")
            print(pic_url?.thumbnail_pic)
            // 判断是否为nil 而且后缀为gif （考虑到cell复用问题）
            if let thPic = pic_url?.thumbnail_pic where thPic.hasSuffix(".gif"){
                gifImageView.hidden = false
            }else {
                gifImageView.hidden = true
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // 设置视图
    private func setupUI(){
        backgroundColor = RandomColor()
        // 01 添加控件
        contentView.addSubview(bgImageView)
        contentView.addSubview(gifImageView)
        // 02 添加约束
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
    }

    // 背景图片
    private lazy var bgImageView: UIImageView = {
        let img = UIImageView(imageName: "weibo_placeholder")
        img.contentMode = .ScaleAspectFill
        img.layer.masksToBounds = true
        return img
    }()
    
    // 动态图片logo
    private lazy var gifImageView: UIImageView = UIImageView(imageName: "timeline_image_gif")
    
    
}


